package cn.wolfcode.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    @RequestMapping("/list")
    @RequiredPermission(name="角色显示",expression ="role:list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){
        PageInfo<Role> pageInfo = roleService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/role/list";
    }
    @RequestMapping("/delete")
    @RequiredPermission(name="角色删除",expression ="role:delete")
    public String delete(Long id){
        if (id != null) {
            roleService.delete(id);
        }
        return "redirect:/role/list";
    }

    @RequestMapping("/input")
    @RequiredPermission(name="角色新增/编辑",expression ="role:saveOrUpdate")
    public String input(Long id,Model model){
        model.addAttribute("permissions",permissionService.listAll());
        if (id != null) {
            Role role = roleService.listOne(id);
            model.addAttribute("role",role);
        }
        return "/role/input";
    }
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="角色新增/编辑",expression ="role:saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids){
        if (role.getId() != null) {
            roleService.update(role,ids);
        } else {
            roleService.insert(role,ids);
        }
        return "redirect:/role/list";
    }
}
